package com.jhs.kjs.model.daumMap.dto;

import java.util.Calendar;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jhs.kjs.dto.util.abstractDTO;
import com.jhs.kjs.model.daumMap.frame.ExtDirection;
import com.jhs.kjs.model.daumMap.frame.ILatLng;

public class DaumMapDTO extends abstractDTO implements ILatLng {
	private static final Logger logger = LoggerFactory
			.getLogger(DaumMapDTO.class);
	
	private long startTimeUnix;
	private long endTimeUnix;
	
	private String date;
	
	private int yy;
	private int mm;
	private int dd;
	private int hh_start;
	private int mm_start;
	private int hh_end;
	private int mm_end;
	
	
	private static final double[] MapWapper = {1.00005, 
		1+(0.00003),
		1+(0.00003*1.5),
		1+(0.00003*2.5),
		1+(0.00003*5),
		1+(0.00003*5*2.5),
		1+(0.0003*5),
		1+(0.0003*5*2),
		1+(0.0003*5*4),
		1+(0.0003*5*8),
		1+(0.0003*5*16),
		1+(0.0003*5*32),
		1+(0.0003*5*64),
		1+(0.0003*5*128),
		1+(0.0003*5*128)};

	private int zoom; // 1 ~ 14

	private Map<String, Double> southWest;
	private Map<String, Double> northEast;
	private double lat;
	private double lng;

	private double minLat;
	private double minLng;
	private double maxLat;
	private double maxLng;

	private int direction;

	/**
	 * south west lat
	 */
	private double swLat;
	/**
	 * south west lng
	 */
	private double swLng;

	/**
	 * north east lat
	 */
	private double neLat;
	/**
	 * north east lng
	 */
	private double neLng;

	/**
	 * south west lat
	 */
	private double lastSwLat;
	/**
	 * south west lng
	 */
	private double lastSwLng;

	/**
	 * north east lat
	 */
	private double lastNeLat;
	/**
	 * north east lng
	 */
	private double lastNeLng;

	private Long CarCode;
	private Long carGroupCode;
	
	private int minPeriod = 2;
	private int secPeriod;

	private int period;

	
	private Paging paging = new Paging();

	public Long getCarCode() {
		return CarCode;
	}

	public void setCarCode(Long carCode) {
		CarCode = carCode;
	}

	public Map<String, Double> getSouthWest() {
		return southWest;
	}

	public void setSouthWest(Map<String, Double> southWest) {
		this.southWest = southWest;

	}

	public Map<String, Double> getNorthEast() {
		return northEast;
	}

	public void setNorthEast(Map<String, Double> northEast) {
		this.northEast = northEast;
	}

	@Override
	public Double getLat() {
		return lat;
	}

	@Override
	public void setLat(Double lat) {
		setLat((double) lat);
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	@Override
	public Double getLng() {
		return lng;
	}

	@Override
	public void setLng(Double lng) {
		this.lng = lng;
	}

	public void setLng(double lng) {
		setLng((Double) lng);
	}

	public boolean contain(ILatLng latLng) {
		
		lat = latLng.getLat();
		lng = latLng.getLng();
		
		if (getSwLat() / MapWapper[getZoom()] <= lat && lat <= getNeLat() * MapWapper[getZoom()]) {
			if (getSwLng() / MapWapper[getZoom()] <= lng && lng <= getNeLng() * MapWapper[getZoom()]) {
				return true;
			}
		}
		return false;
	}

	public int getZoom() {
		return zoom;
	}

	public void setZoom(int zoom) {
		this.zoom = zoom;
	}

	@Override
	public int getCardinal() {
		return ExtDirection.getCardinal(getDirection());
	}

	@Override
	public int getDirection() {
		return direction;
	}

	@Override
	public void setDirection(int direction) {
		this.direction = direction;
	}

	public double getSwLat() {
		return swLat;
	}

	public void setSwLat(double swLat) {
		this.swLat = swLat;
	}

	public double getSwLng() {
		return swLng;
	}

	public void setSwLng(double swLng) {
		this.swLng = swLng;
	}

	public double getNeLat() {
		return neLat;
	}

	public void setNeLat(double neLat) {
		this.neLat = neLat;
	}

	public double getNeLng() {
		return neLng;
	}

	public void setNeLng(double neLng) {
		this.neLng = neLng;
	}

	// swLat=37.45192245844355
	// swLng=126.87287121732173
	// neLat=37.4599318470254
	// neLng=126.90041417702987
	public double getMinLat() {
		// if(getLastNeLat()>0){
		// return swLat/MapWapper[getZoom()]<getLastNeLat()?getLastNeLat():swLat;
		// }
		return swLat / MapWapper[getZoom()];
	}

	public double getMinLng() {
		// if(getLastNeLat()>0){
		// return swLng/MapWapper[getZoom()]<getLastNeLat()?getLastNeLat():swLng;
		// }
		return swLng / MapWapper[getZoom()];
	}

	public double getMaxLat() {
		// if(getLastNeLat()>0){
		// return neLat*MapWapper[getZoom()]<getLastNeLat()?getLastNeLat():neLat;
		// }
		return neLat * MapWapper[getZoom()];
	}

	public double getMaxLng() {
		// if(getLastNeLng()>0){
		// return neLng*MapWapper[getZoom()]<getLastNeLng()?getLastNeLng():neLng;
		// }
		return neLng * MapWapper[getZoom()];
	}

	public double getLastSwLat() {
		return lastSwLat;
	}

	public void setLastSwLat(double lastSwLat) {
		this.lastSwLat = lastSwLat;
	}

	public double getLastSwLng() {
		return lastSwLng;
	}

	public void setLastSwLng(double lastSwLng) {
		this.lastSwLng = lastSwLng;
	}

	public double getLastNeLat() {
		return lastNeLat;
	}

	public void setLastNeLat(double lastNeLat) {
		this.lastNeLat = lastNeLat;
	}

	public double getLastNeLng() {
		return lastNeLng;
	}

	public void setLastNeLng(double lastNeLng) {
		this.lastNeLng = lastNeLng;
	}

	public int getMinPeriod() {
		return minPeriod;
	}

	public void setMinPeriod(int minPeriod) {
		this.minPeriod = minPeriod;
	}

	public int getSecPeriod() {
		return secPeriod;
	}

	public void setSecPeriod(int secPeriod) {
		this.secPeriod = secPeriod;
	}

	public void setMinLat(double minLat) {
		this.minLat = minLat;
	}

	public void setMinLng(double minLng) {
		this.minLng = minLng;
	}

	public void setMaxLat(double maxLat) {
		this.maxLat = maxLat;
	}

	public void setMaxLng(double maxLng) {
		this.maxLng = maxLng;
	}

	public double getOriOriMinLat() {
		return swLat;
	}
	public double getOriMinLat() {
		return swLat;
	}

	public double getOriMinLng() {
		return swLng;
	}

	public double getOriMaxLat() {
		return neLat;
	}

	public double getOriMaxLng() {
		return neLng;
	}

	public Paging getPaging() {
		return paging;
	}

	public void setPaging(Paging paging) {
		this.paging = paging;
	}

	public int getPeriod() {
		return period;
	}

	public void setPeriod(int period) {
		this.period = period;
		switch (this.period) {
		case 0: {
			setSecPeriod(10);
			setMinPeriod(0);
			break;
		}
		case 1: {
			setSecPeriod(30);
			setMinPeriod(0);
			break;
		}
		case 2: {
			setSecPeriod(0);
			setMinPeriod(1);
			break;
		}
		case 3: {
			setSecPeriod(0);
			setMinPeriod(2);
			break;
		}
		case 4: {
			setSecPeriod(0);
			setMinPeriod(4);
			break;
		}
		case 5: {
			setSecPeriod(0);
			setMinPeriod(10);
			break;
		}
		case 6: {
			setSecPeriod(0);
			setMinPeriod(30);
			break;
		}

		}

	}

	public long getStartTimeUnix() {
		if(startTimeUnix<=0){
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			startTimeUnix = cal.getTimeInMillis()/1000;
		}
		return startTimeUnix;
	}

	public void setStartTimeUnix(long startTimeUnix) {
		this.startTimeUnix = startTimeUnix;
	}

	public long getEndTimeUnix() {
		if(endTimeUnix<=0){
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			endTimeUnix = cal.getTimeInMillis()/1000;
		}
		return endTimeUnix;
	}

	public void setEndTimeUnix(long endTimeUnix) {
		this.endTimeUnix = endTimeUnix;
	}

	public int getYy() {
		return yy;
	}

	public void setYy(int yy) {
		this.yy = yy;
	}

	public int getMm() {
		return mm;
	}

	public void setMm(int mm) {
		this.mm = mm;
	}

	public int getDd() {
		return dd;
	}

	public void setDd(int dd) {
		this.dd = dd;
	}

	public int getHh_start() {
		return hh_start;
	}

	public void setHh_start(int hh_start) {
		this.hh_start = hh_start;
	}

	public int getMm_start() {
		return mm_start;
	}

	public void setMm_start(int mm_start) {
		this.mm_start = mm_start;
	}

	public int getHh_end() {
		return hh_end;
	}

	public void setHh_end(int hh_end) {
		this.hh_end = hh_end;
	}

	public int getMm_end() {
		return mm_end;
	}

	public void setMm_end(int mm_end) {
		this.mm_end = mm_end;
	}

	public String getDate() {
		return date==null?getToday():date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	private String getToday(){
		Calendar cal = Calendar.getInstance();
		int yyyy = cal.get(Calendar.YEAR);
		int month = (cal.get(Calendar.MONDAY)+1);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String date = yyyy+"-";
		date+=month<10?"0"+month+"-":month+"-";
		date+=day;
		return date;
	}

	@Override
	public Long getEpochtime() {
		// TODO Auto-generated method stub
		return null;
	}

	public Long getCarGroupCode() {
		return carGroupCode;
	}

	public void setCarGroupCode(Long carGroupCode) {
		this.carGroupCode = carGroupCode;
	}
	
	
}
